{% extends "base.html" %}

{% block content %}
<div class="container">
    <h2 class="mb-4">结算管理</h2>
    
    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-3">
                    <input type="text" class="form-control" name="order_id" placeholder="订单号" value="{{ order_id }}">
                </div>
                <div class="col-md-3">
                    <input type="text" class="form-control" name="customer_name" placeholder="客户名称" value="{{ customer_name }}">
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="status">
                        <option value="">结算状态</option>
                        <option value="pending" {% if status == 'pending' %}selected{% endif %}>待处理</option>
                        <option value="processing" {% if status == 'processing' %}selected{% endif %}>处理中</option>
                        <option value="completed" {% if status == 'completed' %}selected{% endif %}>已完成</option>
                        <option value="cancelled" {% if status == 'cancelled' %}selected{% endif %}>已取消</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <select class="form-select" name="payment_status">
                        <option value="">支付状态</option>
                        <option value="unpaid" {% if payment_status == 'unpaid' %}selected{% endif %}>未支付</option>
                        <option value="partial" {% if payment_status == 'partial' %}selected{% endif %}>部分支付</option>
                        <option value="paid" {% if payment_status == 'paid' %}selected{% endif %}>已支付</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">搜索</button>
                </div>
            </form>
        </div>
    </div>

    <div class="mb-3">
        <a href="{{ url_for('finance.settlement_add') }}" class="btn btn-success">
            <i class="fas fa-plus"></i> 新增结算
        </a>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>结算单号</th>
                    <th>订单号</th>
                    <th>客户名称</th>
                    <th>金额</th>
                    <th>结算状态</th>
                    <th>支付状态</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for settlement in settlements %}
                <tr>
                    <td>{{ settlement.settlement_no }}</td>
                    <td>{{ settlement.order_id }}</td>
                    <td>{{ settlement.customer_name }}</td>
                    <td>¥{{ "%.2f"|format(settlement.amount|float) }}</td>
                    <td>
                        {% if settlement.status == 'pending' %}
                            <span class="badge bg-warning">待处理</span>
                        {% elif settlement.status == 'processing' %}
                            <span class="badge bg-primary">处理中</span>
                        {% elif settlement.status == 'completed' %}
                            <span class="badge bg-success">已完成</span>
                        {% else %}
                            <span class="badge bg-secondary">已取消</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if settlement.payment_status == 'unpaid' %}
                            <span class="badge bg-danger">未支付</span>
                        {% elif settlement.payment_status == 'partial' %}
                            <span class="badge bg-warning">部分支付</span>
                        {% else %}
                            <span class="badge bg-success">已支付</span>
                        {% endif %}
                    </td>
                    <td>{{ settlement.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>
                        <a href="{{ url_for('finance.settlement_edit', id=settlement.id) }}" class="btn btn-sm btn-primary">
                            <i class="fas fa-edit"></i> 编辑
                        </a>
                        <a href="{{ url_for('finance.payment_add') }}?settlement_id={{ settlement.id }}" class="btn btn-sm btn-success">
                            <i class="fas fa-plus"></i> 添加付款
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if pagination.pages > 1 %}
    <nav aria-label="Page navigation">
        <ul class="pagination justify-content-center">
            {% for page in pagination.iter_pages() %}
                {% if page %}
                    <li class="page-item {% if page == pagination.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('finance.settlement_list', page=page, order_id=order_id, customer_name=customer_name, status=status, payment_status=payment_status) }}">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                {% endif %}
            {% endfor %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %} 